var CustomerGuide = {
    id: "CustomerGuideTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

function _formatter(value, row, index) {
    if (row.accountArrears > 0) {
        return '<span style="color: red" title="' + value + '">' + value + '</span>'
    } else {
        return '<span title="' + value + '">' + value + '</span>'
    }
}

function _formatterTime(value, row, index) {
    if (row.accountArrears > 0) {
        return "<span style='color: red'>" + value.substring(0, 19) + "</span>"
    } else {
        return value.substring(0, 19);
    }
}

CustomerGuide.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
        {title: '病历号', field: 'medicalRecordNumber', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.status === "到店") {
                    return "<a href='javascript:;' style='color: green;display: flex;align-items: center;' onclick = 'CustomerGuide.reservationsArrives(" + '"' + row.reservationsId + '"' + ")' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                } else {
                    return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CustomerGuide.reservationsNotDetail(" + '"' + row.reservationsId + '"' + ")' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                }
            }
        },
        {title: '电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 140,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '电话2', field: 'customerPhone2', align: 'center', valign: 'middle', sortable: true, width: 135,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '使用人', field: 'phoneSharShip', align: 'center', valign: 'middle', sortable: true, width: 90,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '性别', field: 'sex', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '科室', field: 'treatmentUnit', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '二级渠道', field: 'twoChannel', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '三级渠道', field: 'thrChannel', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '预约TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '到店TC', field: 'fieldTcdd', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '导医', field: 'guideDoctor', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '开始时间', field: 'startTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return _formatterTime(value, row, index);
            }
        },
        {title: '结束时间', field: 'endTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return _formatterTime(value, row, index);
            }
        },
        {title: '市场确认时间', field: 'affirmTimesc', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '导医确认时间', field: 'affirmTimedy', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '到店时间', field: 'arrivalTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '黄页编号', field: 'yellowNumber', align: 'center', valign: 'middle', sortable: true, width: 140,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            },
            cellStyle: function (value, row, index) {
                return {css: {"overflow":"hidden","white-space":"nowrap","text-overflow":"ellipsis"}}
            }
        },
        {title: '网电咨询师', field: 'scarchiving', align: 'center', valign: 'middle', sortable: true, width: 130,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '一级媒介', field: 'oneMedium', align: 'center', valign: 'middle', sortable: true, width: 130,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '二级媒介', field: 'twoMedium', align: 'center', valign: 'middle', sortable: true, width: 130,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '三级媒介', field: 'thrMedium', align: 'center', valign: 'middle', sortable: true, width: 130,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        }
    ];
    return columns;
};
//表单查询提交参数
CustomerGuide.formParams = function () {
    var queryData = {};
    queryData['sreach'] = $("#sreach").val();
    queryData['CustomerGuideName'] = $("#CustomerGuideName").val();
    queryData['CustomerGuidePhone'] = $("#CustomerGuidePhone").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['reservations'] = $("#reservations").val();
    queryData['startRevisitTime'] = $("#startRevisitTime").val();
    queryData['endRevisitTime'] = $("#endRevisitTime").val();
    queryData['fieldTc'] = $("#fieldTc").val();
    queryData['doctor'] = $("#doctor").val();
    queryData['customerStatus'] = $("#customerStatus").val();
    queryData['treatmentUnit'] = $("#treatmentUnit").val();
    queryData['isSC'] = $("#isSC").val();
    queryData['isDY'] = $("#isDY").val();
    queryData['oneChannel'] = multipleSelection($('#oneChannel').val());
    queryData['twoChannel'] = multipleSelection($('#twoChannel').val());
    queryData['thrChannel'] = multipleSelection($('#thrChannel').val());
    queryData['abbreviation'] = $("#abbreviation", parent.document).val();
    return queryData;
};
//把对象拼接成URL的键值对参数 param1=value1&param2=value2
var parseParam = function (param) {
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};
var getSingleData = function (param, key) {
    return key + "=" + param[key];
};
//重置
CustomerGuide.resetSearch = function () {
    $("#sreach").val("");
    $("#startTime").val("");
    $("#endTime").val("");
    $("#fieldTc").val("");
    $("#doctor").val("");
    $("#customerStatus").val("");
    $('#oneChannel').selectpicker('val', ['noneSelectedText'])//回到初始状态
    $('#twoChannel').selectpicker('val', ['noneSelectedText'])//回到初始状态
    $('#thrChannel').selectpicker('val', ['noneSelectedText'])//回到初始状态
    $(".timeBox > div button").removeClass('actionClick')
    CustomerGuide.search();
};
CustomerGuide.reservationsArrives = function (id) { // 点击表格姓名进入详情 - 已到
    var index = layer.open({
        type: 2,
        title: '到店客户详情',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset: ['0px', '40%'],
        content: Feng.ctxPath + '/reservationsArrive/reservations_arrives/' + id
    });
    this.layerIndex = index;
};

CustomerGuide.reservationsNotDetail = function (id) { // 点击表格姓名进入详情 - 未到
    var pageType = $("#pageType").val();
    window.localStorage.setItem("c", pageType);
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset: ['0px', '40%'],
        content: Feng.ctxPath + '/reservationsNotArrive/to_reservationsNotDetail/' + id
    });
    this.layerIndex = index;
};

//关闭窗口
CustomerGuide.close = function () {parent.layer.close(parent.layer.getFrameIndex(window.name));};
// 检查是否选中
CustomerGuide.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {Feng.info("请先选中表格中的某一记录！");return false;}
    else {CustomerGuide.seItem = selected[0];return true;}
};
//刷新 + 统计
CustomerGuide.search = function () {CustomerGuide.table.refresh({query: CustomerGuide.formParams()});CustomerGuide.total();};
//统计
CustomerGuide.total = function () {
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    var valueOne = multipleSelection($('#oneChannel').val());
    var valueTwo = multipleSelection($('#twoChannel').val());
    var valueThr = multipleSelection($('#thrChannel').val());
    var fieldTc = $("#fieldTc").val();
    var doctor = $("#doctor").val();
    var treatmentUnit = $("#treatmentUnit").val();
    var sreach = $("#sreach").val();
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/customerGuide/total",
        data: {
            startTime: startTime,
            endTime: endTime,
            oneChannelValue: valueOne,
            twoChannelValue: valueTwo,
            thrChannelValue: valueThr,
            fieldTc: fieldTc,
            doctor: doctor,
            treatmentUnit: treatmentUnit,
            sreach: sreach,
        },
        dataType: "json",
        success: function (data) {
            $("#total").html(data[0].countNum);//总人数
            $("#further").html(data[1].countNum);//复诊预约
            $("#new").html(data[2].countNum);//新诊预约
        }
    });
};
//点击上方条件筛选表格
CustomerGuide.totalNumber = function (ojb) {
    document.getElementById("customerStatus").value = ojb;
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['oneChannel'] = multipleSelection($('#oneChannel').val());
    queryData['twoChannel'] = multipleSelection($('#twoChannel').val());
    queryData['thrChannel'] = multipleSelection($('#thrChannel').val());
    queryData['sreach'] = $("#sreach").val();
    queryData['customerStatus'] = ojb;
    CustomerGuide.search();
};
// 明日
CustomerGuide.tomorrow = function () {
    $("#startFilingTime").val(setDateTime(new Date(),-1) + " " + "00:00:00");
    $("#endFilingTime").val(setDateTime(new Date(),-1) + " " + "23:59:59");
    $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick');
    CustomerGuide.search();
};
// 点击上面状态筛选表格
CustomerGuide.today = function () {
    $("#startTime").val(setDateTime(new Date(),0) + " " + "00:00:00");
    $("#endTime").val(setDateTime(new Date(),0) + " " + "23:59:59");
    CustomerGuide.search();
};
// 明天
CustomerGuide.tomorrow = function () {
    $("#startTime").val(setDateTime(new Date(),-1) + " " + "00:00:00");
    $("#endTime").val(setDateTime(new Date(),-1) + " " + "23:59:59");
    CustomerGuide.search();
};
// 本周
CustomerGuide.thisWeek = function () {
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {stepSunDay = -7;}
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    $("#startTime").val(setDateTime(monday,0) + " " + "00:00:00");
    $("#endTime").val(setDateTime(sunday,0) + " " + "23:59:59");
    CustomerGuide.search();
};

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#CustomerGuideTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}
$(function () {
    var defaultColunms = CustomerGuide.initColumn();
    var table = new BSTable(CustomerGuide.id, "/customerGuide/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(CustomerGuide.formParams());
    CustomerGuide.table = table.init();
    menuNameHideColumn($("#menuName").val(),'CustomerGuideTable');
    $("#oneChannel").change(function () {$("#twoChannel").val('请选择');$("#thrChannel").val('请选择');CustomerGuide.search();});
    $("#twoChannel").change(function () {$("#thrChannel").val('请选择');CustomerGuide.search();});
    $("#thrChannel").change(function () {CustomerGuide.search();});
    $(document).on('click','#laydate_clear,#laydate_today,#laydate_ok',function () {CustomerGuide.search();});
    checkDate = function () {CustomerGuide.search();};
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    $("#totalCount").addClass('actionClick').siblings().removeClass('actionClick');
    CustomerGuide.today();
    phonePrivileges();
});